﻿///-----------------------------------------------------------------------
/// <copyright file="PeriodsBudgetSource.cs" company="FactSet Research Systems, Inc.">
///     Copyright © FactSet Research Systems, Inc.  All rights reserved 2012.
/// </copyright>
/// <author>Jonathan K. Barez</author>
///-----------------------------------------------------------------------
namespace HousholdISv2.ViewModel.Transaction
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Core;
    using MVVMCore.ViewModel;
using MVVMCore.ViewModel.Details;
using HousholdISv2.DataRepository;
using System.Windows;
    using System.Windows.Input;

    public class PeriodsBudgetSource : UpdateableViewModelBase, IHasItemCollection
    {
        #region Fields

        private readonly PeriodsViewModel parent;

        #endregion Fields

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="PeriodsBudgetSource"/> class.
        /// </summary>
        public PeriodsBudgetSource(PeriodsViewModel parent)
        {
            this.parent = parent;
        }
        #endregion Constructors

        #region Events

        #endregion Events

        #region Properties

        public string DisplayName
        {
            get { return "Budget Source"; }
        }

        public bool IsSelected
        {
            get { return GetValue(() => IsSelected); }
            set { SetValue(() => IsSelected, value); }
        }

        public  bool IsReadOnly
        {
            get { return this.IsInSelectionMode; }
            set { this.IsInSelectionMode = value; }
        }

        public Visibility DetailsVisibility
        {
            get
            {
                return this.Sources.SelectedData == null
                           ? Visibility.Collapsed
                           : Visibility.Visible;
            }
        }

        public bool IsInSelectionMode
        {
            get { return GetValue(() => this.IsInSelectionMode, true); }
            set
            {
                SetValue(() => this.IsInSelectionMode, value);
                this.RaisePropertyChangedEvent(() => this.IsInSelectionMode);
            }
        }

        public ListViewViewModel<PeriodBudget> Sources
        {
            get { return GetValue(() => this.Sources) ?? this.InitializeSources(); }
            set { SetValue(() => this.Sources, value); }
        }

        public  string Comment
        {
            get { return GetValue(() => this.Comment); }
            set { SetValue(() => this.Comment, value); }
        }

        public decimal Amount
        {
            get { return GetValue(() => this.Amount); }
            set { SetValue(() => this.Amount, value); }
        }

        protected override bool CanEdit
        {
            get { return this.Sources.SelectedData != null; }
        }

        protected override bool CanDelete
        {
            get { return this.Sources.SelectedData != null; }
        }

        protected override bool CanSave
        {
            get
            {
                return !string.IsNullOrWhiteSpace(this.Comment) &&
                       this.Amount != 0;
            }
        }

        #endregion Properties

        #region Methods

        protected override void DoCreateNew()
        {
            throw new NotImplementedException();
        }

        protected override void DoSave()
        {
            throw new NotImplementedException();
        }

        protected override void DoCancel()
        {
            throw new NotImplementedException();
        }

        private ListViewViewModel<PeriodBudget> InitializeSources()
        {
            var lvvm = new ListViewViewModel<PeriodBudget>();
            lvvm.SelectionChanged +=
                (sender, e) => this.LoadDetails();
            return this.Sources = lvvm;
        }

        private void LoadDetails()
        {
            this.RaisePropertyChangedEvent(() => this.DetailsVisibility);
            if (this.Sources.SelectedData == null)
            {
                return;
            }

            var data = this.Sources.SelectedData;
            this.Comment = data.Comment;
            this.Amount = data.Amount;
            CommandManager.InvalidateRequerySuggested();
        }

        #endregion Methods
    }
}
